{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[151 149 147 ... 167 170 174]\n",
      " [148 145 143 ... 167 170 173]\n",
      " [150 149 149 ... 163 164 165]\n",
      " ...\n",
      " [ 59  62  67 ...  86  82  81]\n",
      " [ 59  63  65 ...  86  82  79]\n",
      " [ 62  65  67 ...  93  88  84]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "path = '/home/zqh/Downloads/bb/FILE.raw'\n",
    "w, h = np.fromfile(path, dtype=np.uint32)[:2]\n",
    "src = np.fromfile(path, dtype=np.uint8)[8:].reshape(h,w)\n",
    "print(src)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   0    1    4   11   27   47  144  281  497  403  458  487  505  481\n",
      "  443  448  505  479  498  561  545  528  620  587  605  607  677  662\n",
      "  655  594  582  597  605  575  572  559  610  530  590  597  567  612\n",
      "  562  527  572  601  599  589  644  667  650  660  684  699  753  695\n",
      "  645  696  679  625  684  664  630  623  608  603  610  623  661  635\n",
      "  659  655  704  664  717  724  756  791  778  806  798  790  823  846\n",
      "  828  949  967  880  911  956  978 1121 1129 1126 1164 1223 1191 1245\n",
      " 1235 1255 1308 1245 1244 1174 1066 1045 1002 1042 1008  933  896  891\n",
      "  836  745  711  653  557  497  407  417  364  356  326  324  287  303\n",
      "  304  340  302  317  318  373  371  397  438  434  435  522  539  610\n",
      "  688  727  765  793  892  980  964 1078 1158 1237 1285 1223 1322 1332\n",
      " 1331 1317 1275 1259 1142 1183 1090 1039 1068  950  943  784  739  742\n",
      "  666  560  566  537  489  486  424  374  404  393  362  349  365  308\n",
      "  329  271  270  276  230  231  268  265  210  217  232  227  228  260\n",
      "  244  236  252  268  271  311  276  310  318  364  342  396  396  392\n",
      "  430  435  447  481  494  407  411  410  395  361  380  348  386  325\n",
      "  333  296  265  312  267  272  232  233  239  266  231  252  269  254\n",
      "  246  217  233  225  213  207  218  171  171  150  175  188  210  219\n",
      "  246  309  375  523]\n"
     ]
    }
   ],
   "source": [
    "hist = np.zeros(256, dtype=int)\n",
    "for i in range(h):\n",
    "    for j in range(w):\n",
    "        hist[src[i][j]] += 1\n",
    "print(hist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[     0      1      5     16     43     90    234    515   1012   1415\n",
      "   1873   2360   2865   3346   3789   4237   4742   5221   5719   6280\n",
      "   6825   7353   7973   8560   9165   9772  10449  11111  11766  12360\n",
      "  12942  13539  14144  14719  15291  15850  16460  16990  17580  18177\n",
      "  18744  19356  19918  20445  21017  21618  22217  22806  23450  24117\n",
      "  24767  25427  26111  26810  27563  28258  28903  29599  30278  30903\n",
      "  31587  32251  32881  33504  34112  34715  35325  35948  36609  37244\n",
      "  37903  38558  39262  39926  40643  41367  42123  42914  43692  44498\n",
      "  45296  46086  46909  47755  48583  49532  50499  51379  52290  53246\n",
      "  54224  55345  56474  57600  58764  59987  61178  62423  63658  64913\n",
      "  66221  67466  68710  69884  70950  71995  72997  74039  75047  75980\n",
      "  76876  77767  78603  79348  80059  80712  81269  81766  82173  82590\n",
      "  82954  83310  83636  83960  84247  84550  84854  85194  85496  85813\n",
      "  86131  86504  86875  87272  87710  88144  88579  89101  89640  90250\n",
      "  90938  91665  92430  93223  94115  95095  96059  97137  98295  99532\n",
      " 100817 102040 103362 104694 106025 107342 108617 109876 111018 112201\n",
      " 113291 114330 115398 116348 117291 118075 118814 119556 120222 120782\n",
      " 121348 121885 122374 122860 123284 123658 124062 124455 124817 125166\n",
      " 125531 125839 126168 126439 126709 126985 127215 127446 127714 127979\n",
      " 128189 128406 128638 128865 129093 129353 129597 129833 130085 130353\n",
      " 130624 130935 131211 131521 131839 132203 132545 132941 133337 133729\n",
      " 134159 134594 135041 135522 136016 136423 136834 137244 137639 138000\n",
      " 138380 138728 139114 139439 139772 140068 140333 140645 140912 141184\n",
      " 141416 141649 141888 142154 142385 142637 142906 143160 143406 143623\n",
      " 143856 144081 144294 144501 144719 144890 145061 145211 145386 145574\n",
      " 145784 146003 146249 146558 146933 147456]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "hist_cdf = copy.deepcopy(hist)\n",
    "for i in range(1, 256):\n",
    "    hist_cdf[i] = hist_cdf[i-1] + hist[i]\n",
    "print(hist_cdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.figure()\n",
    "plt.plot(hist_cdf)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
